//
//  FenLeiViewController.m
//  HWanGame
//
//  Created by Chendaobin on 2019/8/23.
//  Copyright © 2019年 Chendaobin. All rights reserved.
//

#import "FenLeiViewController.h"
#import "HWFenLeiYouXiCell.h"
#import "HWJinXuanCell.h"
#import "HWOnlinegameTableCell.h"
#import "HWFeiLeimodel.h"
#import "HWGameListModel.h" // 游戏列表
 
#import "HWFenleishitTuViewController.h"

@interface FenLeiViewController ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(strong,nonatomic) UITableView *mainaTableVie;
@property(strong,nonatomic) UICollectionView *maincollectionView;
@property(strong,nonatomic) NSMutableArray *feileiMuArr; // 左边分类
@property(strong,nonatomic) UITableView *otherTableVie;
@property(strong,nonatomic) NSIndexPath *selectedIndex;

@property(strong,nonatomic) NSMutableArray *dataMuArr;
@property(strong,nonatomic) NSString *p; //分页
@property(strong,nonatomic) NSString *max_p; // 最大分页
@property(strong,nonatomic) HWFeiLeimodel *selectedModel;



@end

@implementation FenLeiViewController


- (UITableView *)otherTableVie {
    
    if (_otherTableVie == nil) {
        _otherTableVie = [[UITableView alloc]initWithFrame:CGRectMake(78*CFLScaleValue, 0,kScreenW-78*CFLScaleValue, kScreenH-TabBarHeight) style:UITableViewStylePlain];
        _otherTableVie.delegate = self;
        _otherTableVie.dataSource = self;
        _otherTableVie.tag = 100;
        _otherTableVie.hidden = YES;
        _otherTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _otherTableVie.backgroundColor = [UIColor whiteColor];
    }
    
    return  _otherTableVie;
    
    
}

-(UITableView *)mainaTableVie {
    
    if (_mainaTableVie == nil
        ) {
        
        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 78*CFLScaleValue, kScreenH-TabBarHeight) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
        
        
        
    }
    return   _mainaTableVie ;
    
}






- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor yellowColor];
    
    //设置长用的属性
    [self setupshuxing];
    // 添加UI试图
    [self setupUI];
    //添加控件
    [self addControlsview];
    //  添加OtheView
    [self addOthetaview];
    //添加刷新控件
    [self addrefreshControl];
    
    
    
    if ([self.judgeStr isEqualToString:@"1"]) {
        [self recalculateCoordinates];
        
    }
    
    //查询分类
    [self chaxunqueryClassification];
    
    
}


- (void)addrefreshControl {
    
   self.maincollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
  //  [self.mainaTableVie.mj_header beginRefreshing];
    self.maincollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
    
    self.otherTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    self.otherTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
    
    
    
}




#pragma  mark ------
//下拉刷新
- (void)underRefresh {
    DLog(@"精选");
    
    
    HWFeiLeimodel *model =  self.selectedModel;
    self.p  = @"1";
    
    self.dataMuArr = [[NSMutableArray alloc]init];
    
    NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"fenlei_game",
                                @"type": model.id,
                                @"p"  : self.p,
                                @"ios" : @"1"
                                };
    
    
  
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
       
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        [self.otherTableVie.mj_header endRefreshing];
        [self.otherTableVie.mj_footer endRefreshing];
        
        
        if ([codeStr intValue] == 1) {
          //  [MBProgressHUD  showError:@"加载成功"];
                NSArray *arr = response[@"data"];
          
                self.dataMuArr = [HWGameListModel mj_objectArrayWithKeyValuesArray:arr];
                
                self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                
                self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                
                [self.maincollectionView reloadData];
                [self.otherTableVie reloadData];
                


          
            
            
        } else {
            
            self.dataMuArr = [[NSMutableArray alloc]init];
            
            [MBProgressHUD showError:response[@"message"]];
            
            
            
            [self.maincollectionView reloadData];
            [self.otherTableVie reloadData];
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        [self.otherTableVie.mj_header endRefreshing];
        [self.otherTableVie.mj_footer endRefreshing];
        
        
        
    }];
    
    
    
    
    
    
    
}
//  上拉刷新

- (void)onRefresh {
    
    DLog(@"精选");
    
    
    HWFeiLeimodel *model =  self.selectedModel;
    
    
    NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"fenlei_game",
                                @"type": model.id,
                                @"p"  : self.p,
                                @"ios" : @"1"
                                };
    
    
    
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        [self.otherTableVie.mj_header endRefreshing];
        [self.otherTableVie.mj_footer endRefreshing];
        
        if ([codeStr intValue] == 1) {
          //  [MBProgressHUD  showError:@"加载成功"];
            NSArray *arr = response[@"data"];
            if (arr.count > 0) {
                
                
              NSMutableArray  *muArr  = [HWGameListModel mj_objectArrayWithKeyValuesArray:arr];
                
                for (int i = 0; i < muArr.count; i++) {
                    
                    [self.dataMuArr addObject:muArr[i]];
                    
                }
                
                self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                
                self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
            
                [self.maincollectionView reloadData];
                [self.otherTableVie reloadData];
                
            }
            
            
            
            
        } else {
            
            
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        
        [self.maincollectionView.mj_header endRefreshing];
        [self.maincollectionView.mj_footer endRefreshing];
        [self.otherTableVie.mj_header endRefreshing];
        [self.otherTableVie.mj_footer endRefreshing];
        
    }];
    
    
    
}










// 查询分类
- (void)chaxunqueryClassification {
    
    
    NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"fenlei",
                                @"ios" : @"1"
                                };
    
  //  [MBProgressHUD show:<#(NSString *)#>]
  //  [MBProgressHUD showMessage:@"正在加载" toView:nil];
    
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
      
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        [MBProgressHUD hideHUD];
        
        
        if ([codeStr intValue] == 1) {
         //   [MBProgressHUD  showError:@"加载成功"];
            //  HWFeiLeimodel
            NSArray *arr =  response[@"data"];
            self.feileiMuArr = [HWFeiLeimodel mj_objectArrayWithKeyValuesArray:arr];
            [self.mainaTableVie reloadData];
            
            if (self.feileiMuArr.count > 0) {
                HWFeiLeimodel *model = self.feileiMuArr[0];
                self.dataMuArr = [[NSMutableArray alloc]init];
                self.selectedModel  = model;
                
                [self fenleiyouxiID:model.id];
                
                
            }
           
            
    

        } else {
            
             [MBProgressHUD  showError:@"加载失败"];
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
      
        [MBProgressHUD  showError:@"加载失败"];
        
        
            
    }];
    
    
    
    
}

//查询分类详情
- (void)fenleiyouxiID:(NSString *)IdStr {
    
    
    [MBProgressHUD showMessage:@"正在加载" toView:nil];
    
    
    NSDictionary *parametes = @{
                                @"ct" :  @"app",
                                @"ac" : @"fenlei_game",
                                @"type": IdStr,
                                @"p"  :@"1",
                                @"ios" : @"1"
                                };
    
    [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
        
        
        NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
        [MBProgressHUD hideHUD];
        
        
        if ([codeStr intValue] == 1) {
            [MBProgressHUD  showError:@"加载成功"];
            NSArray *arr = response[@"data"];
            if (arr.count > 0) {
                
                
                self.dataMuArr = [HWGameListModel mj_objectArrayWithKeyValuesArray:arr];
                
                self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                
                self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                
                
                
                
                [self.maincollectionView reloadData];
            }
          
        // HWGameListModel
            
        //   self.latestMutaBleArr  = [HWGameListModel mj_objectArrayWithKeyValuesArray:latestArr];
            
            
            
        } else {
            
             [MBProgressHUD  showError:@"加载失败"];
            
        }
        NSLog(@"%@",response);
        
        
    } failure:^(NSString *error) {
        
        [MBProgressHUD  showError:@"加载失败"];
        
    }];
    
    
    
    
    
}





// 重新做坐标
- (void)recalculateCoordinates {
    self.otherTableVie.frame = CGRectMake(78*CFLScaleValue, 0,kScreenW-78*CFLScaleValue, kScreenH);
    self.mainaTableVie.frame = CGRectMake(0, 0, 78*CFLScaleValue, kScreenH);
    self.maincollectionView.frame  =  CGRectMake(78*CFLScaleValue, 0, kScreenW-78*CFLScaleValue, kScreenH);
    
}

//  添加OtheyTabview
- (void)addOthetaview {
    
    
    [self.view addSubview:self.otherTableVie];
}

- (void)setupshuxing {
    
    self.navigationItem.title = @"游戏大厅";
   // self.feileiArr = @[@"精选",@"卡牌",@"竞技",@"生存",@"休闲",@"仙侠",@"动作",@"放置",@"角色",@"卡牌",@"竞技",@"生存",@"休闲",@"仙侠",@"动作",@"放置",@"角色"];
    
    
    
}

//设置UI
- (void)setupUI {
    
     [self.view addSubview:self.mainaTableVie];
    
}

#pragma  mark -----  添加控件
- (void)addControlsview {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 18;
    
    CGFloat juanju  = (kScreenW - 24*CFLScaleValue*2-62*CFLScaleValue*3-78*CFLScaleValue)/2;
    layout.minimumInteritemSpacing = juanju-1;
    layout.headerReferenceSize = CGSizeMake(kScreenW, 142);  //设置headerView大小
    self.maincollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(78*CFLScaleValue, 0, kScreenW-78*CFLScaleValue, kScreenH-TabBarHeight) collectionViewLayout:layout];
    // self.maincollectionView.showsHorizontalScrollIndicator = NO;
    self.maincollectionView.delegate = self;
    self.maincollectionView.dataSource = self;
    self.maincollectionView.alwaysBounceVertical = YES;
    self.maincollectionView.backgroundColor = [UIColor whiteColor];
   // self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 15, 0, 15);
    self.maincollectionView.contentInset = UIEdgeInsetsMake(0, 24*CFLScaleValue, 0, 24*CFLScaleValue);
    [self.maincollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];  //  一定要设置
  //  [self.maincollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cellID"];
    [self.maincollectionView  registerNib:[UINib nibWithNibName:@"HWJinXuanCell" bundle:nil] forCellWithReuseIdentifier:@"cellID"];
    self.maincollectionView.userInteractionEnabled = YES;
    [self.view  addSubview:self.maincollectionView];
    
    
    
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    if (tableView.tag == 100) {
         return   self.dataMuArr.count;
    }
    return   self.feileiMuArr.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView.tag == 100 ) {
        
        static NSString *cellID = @"cellIDOnlinegame";
        
        HWOnlinegameTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell ) {
            cell = [[NSBundle mainBundle]loadNibNamed:@"HWOnlinegameTableCell" owner:nil options:nil].lastObject;
    
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.sizeGameLabel.hidden = YES;
        cell.penfenImageView.hidden = NO;
        cell.penfenLabel.hidden = NO;
        cell.fenleimodel = self.dataMuArr[indexPath.row];
        cell.backgroundColor = [UIColor whiteColor];
    
        return  cell;
        
        
    } else {
        
        
        static NSString *cellID = @"cellID";
        HWFenLeiYouXiCell *cell =  [tableView dequeueReusableCellWithIdentifier:cellID];
        if (!cell) {
            cell =    [[NSBundle mainBundle]loadNibNamed:@"HWFenLeiYouXiCell" owner:nil options:nil].lastObject;
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
         cell.backgroundColor = [UIColor whiteColor];
        HWFeiLeimodel *mode = self.feileiMuArr[indexPath.row];
        
        cell.nameLabel.text =  mode.name;
        cell.backgroundColor = [UIColor whiteColor];
        
        if (self.selectedIndex != nil) {
            if (self.selectedIndex == indexPath) {
                
                cell.xuanzhongview.hidden = NO;
                cell.nameLabel.backgroundColor = [UIColor whiteColor];
            } else {
                
                cell.xuanzhongview.hidden = YES;
                cell.nameLabel.backgroundColor = RGB(247, 247, 247);
            }
            
            
        } else {
            
             if (indexPath.row == 0) {
                    cell.xuanzhongview.hidden = NO;
                    cell.nameLabel.backgroundColor = [UIColor whiteColor];
                    self.selectedIndex = indexPath;
            
            
            
                } else {
                    cell.xuanzhongview.hidden = YES;
                    cell.nameLabel.backgroundColor = RGB(247, 247, 247);
            
                }
            
            
            
            
        }
       
        
        
 
        return  cell;
        
    }
    
  
    
}


#pragma  mark -------------- UITableViewDelegate


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    if (tableView.tag == 100 ) {
        
        return 75;
    }
    return  56;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DLog(@"1234");
    
    
    if (tableView.tag == 100 ) {
        
        
        NSLog(@"daying");
        
        
        HWGameListModel *model = self.dataMuArr[indexPath.row];
        HWGameDetailController *gameDetails = [[HWGameDetailController alloc]init];
        gameDetails.gameId = model.id;
        
        [self.navigationController pushViewController:gameDetails animated:YES];
        
    } else {
        
        
        HWFenLeiYouXiCell *oldcell =  [tableView cellForRowAtIndexPath:self.selectedIndex];
        oldcell.xuanzhongview.hidden = YES;
        oldcell.nameLabel.backgroundColor = RGB(247, 247, 247);
        
        
        HWFenLeiYouXiCell *newcell =  [tableView cellForRowAtIndexPath:indexPath];
        newcell.xuanzhongview.hidden = NO;
        newcell.nameLabel.backgroundColor = [UIColor whiteColor];
        self.selectedIndex = indexPath;
        
        self.selectedModel = self.feileiMuArr[indexPath.row];
        
        
        
        if (indexPath.row == 0) {
            
            self.maincollectionView.hidden = NO;
            self.otherTableVie.hidden = YES;
            
            [self.maincollectionView.mj_header beginRefreshing];
            
            
        } else {
            
            self.maincollectionView.hidden = YES;
            self.otherTableVie.hidden = NO;
            
            [self.otherTableVie.mj_header beginRefreshing];
            
            
        }
        
        
        
    }
        
    //HWFenLeiYouXiCell self.tableView     self.selectedIndex
   
    
    
    
    
}








#pragma mark --------- UICollectionViewDelegate


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (self.dataMuArr.count > 0 ) {
         return self.dataMuArr.count-2;
    }
    return 0;
}


- (UICollectionReusableView *) collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
  //  headerView.backgroundColor = [UIColor redColor];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(-11*CFLScaleValue, 8, 130*CFLScaleValue, 90*CFLScaleValue)];
    imageView.layer.cornerRadius = 6*CFLScaleValue;
    imageView.layer.masksToBounds = YES;
    imageView.image = [UIImage imageNamed:@"c_1"];
    imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTap1)];
    [imageView addGestureRecognizer:tap1];
    
    

    if (self.dataMuArr.count > 0) {
        
        HWGameListModel *mode = self.dataMuArr[0];
        NSURL *url = [NSURL URLWithString:mode.image];
        [imageView sd_setImageWithURL:url placeholderImage:nil];
        
        
        
    }
    


    imageView.userInteractionEnabled = YES;
    [headerView addSubview:imageView];
    
    UIImageView *imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(kScreenW-78*CFLScaleValue-24*CFLScaleValue*2-130*CFLScaleValue+12*CFLScaleValue, 8, 130*CFLScaleValue, 90*CFLScaleValue)];
    imageView1.layer.cornerRadius = 6*CFLScaleValue;
    imageView1.layer.masksToBounds = YES;
    imageView1.image = [UIImage imageNamed:@"c_1"];
    imageView1.userInteractionEnabled = YES;
    [headerView addSubview:imageView1];
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewTap2)];
      [imageView1 addGestureRecognizer:tap2];
    
    
    if (self.dataMuArr.count > 1) {
        HWGameListModel *mode1 = self.dataMuArr[1];
        NSURL *url1 = [NSURL URLWithString:mode1.image];
        [imageView1 sd_setImageWithURL:url1 placeholderImage:nil];
        
    }
    
 
    
    
    UILabel *selectLabel = [[UILabel alloc]initWithFrame:(CGRect)CGRectMake(-9*CFLScaleValue, imageView.bottom+12, 90, 16)];
    selectLabel.font = [UIFont systemFontOfSize:15];
    selectLabel.textColor = RGB(51, 51, 51);
    selectLabel.text = @"精选推荐";
    [headerView addSubview:selectLabel];
     
//    UIButton *butt = [UIButton createNormalButtonWithFrame:CGRectMake(kScreenW-78-24*2-38+8, imageView.bottom+13, 38, 13) Image:@"ba_r" Title:@"更多" font:13 imageSize:CGSizeMake(7, 11)];
//    [butt layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:4];
//    butt.backgroundColor = [UIColor yellowColor];
//    [butt addTarget:self action:@selector(buttTap) forControlEvents:UIControlEventTouchUpInside];
//   // butt
//    [headerView addSubview:butt];
//    
    return headerView;
}




- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    HWJinXuanCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
  //   cell.backgroundColor =[UIColor yellowColor];
    HWGameListModel *mode = self.dataMuArr[indexPath.row+2];
    cell.model = mode;
    
    
    return cell;
    
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(62*CFLScaleValue,12+62*CFLScaleValue+4);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DLog(@"12334");
    
    HWGameDetailController *gameDetails = [[HWGameDetailController alloc]init];
    
    HWGameListModel *mode = self.dataMuArr[indexPath.row+2];
    gameDetails.gameId = mode.id;

    [self.navigationController pushViewController:gameDetails animated:YES];
    
    
}



#pragma mark ------------   点击更多按钮

- (void)buttTap {
    
    
    DLog(@"145678778");
}

// 广告
- (void)imageViewTap1 {
    
    NSLog(@"12345");
    
    HWGameListModel *mode = self.dataMuArr[0];
    HWGameDetailController *gameDetails = [[HWGameDetailController alloc]init];
    gameDetails.gameId = mode.id;
    [self.navigationController pushViewController:gameDetails animated:YES];
    
    
}

- (void)imageViewTap2 {
    
       NSLog(@"67890");
    
    HWGameListModel *mode = self.dataMuArr[1];
    HWGameDetailController *gameDetails = [[HWGameDetailController alloc]init];
    gameDetails.gameId = mode.id;
    [self.navigationController pushViewController:gameDetails animated:YES];
    
    
}

@end
